import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import { Activity } from "../entity/activity";
import { Observable }     from 'rxjs/Observable';
import {AuthService} from "./auth.service";
import {User} from "../entity/user";

@Injectable()
export class ActivityService {

  private httpUrl = 'http://localhost:8080/activity';

  constructor (private http: Http, private authService:AuthService) {}

  private extractData(res: Response) {
    const body = res.json();
    return body || {};
  }

  private handleError (error: any) {
    // In a real world app, we might use a remote logging infrastructure
    // We'd also dig deeper into the error to get a better message
    const errMsg = (error.message) ? error.message :
      error.status ? `${error.status} - ${error.statusText}` : 'Server error';
    console.error(errMsg); // log to console instead
    return Observable.throw(errMsg);
  }

  getActivitys(page:number):Observable<Activity[]> {
    const headers = new Headers({ 'Content-Type': 'application/json' });
    const options = new RequestOptions({ headers: headers });

    return this.http.get(`${this.httpUrl}?page=${page}`, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  insertActivity(activity:Activity):void {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({ headers: headers });

    this.http.post(this.httpUrl+"/secure/insert", activity, options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);

  }

  getActivity(id:string):Observable<Activity> {
    const headers = new Headers({ 'Content-Type': 'application/json' });
    const options = new RequestOptions({ headers: headers });

    return this.http.get(`${this.httpUrl}/${id}`, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  update(activity:Activity){
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({ headers: headers });

    this.http.post(this.httpUrl+"/secure/update", activity, options).toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }

  campaign(activityId:string):Observable<number> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({ headers: headers });

    return this.http.post(this.httpUrl+"/secure/campaign", activityId, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  getUsersByActivityId(activityId:string):Observable<User[]> {
    const headers = new Headers({ 'Content-Type': 'application/json' });
    const options = new RequestOptions({ headers: headers });

    return this.http.get(`${this.httpUrl}/campaignUsers?activityId=${activityId}`, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  getActivitiesByUserId(page:number):Observable<Activity[]> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({ headers: headers });

    return this.http.post(this.httpUrl+"/secure/myActivities", page, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

  delete(activityId: string):Observable<number> {
    const headers = new Headers({
      'Content-Type': 'application/json',
      'authorization': this.authService.getCompactJws()
    });
    const options = new RequestOptions({ headers: headers });

    return this.http.post(this.httpUrl+"/secure/delete", activityId, options)
      .map(this.extractData)
      .catch(this.handleError);
  }

/*  getActivitysAreas():Observable<ActivityArea[]> {
    return this.http.get(this.httpUrl+"/getActivityAreas")
      .map(this.extractData)
      .catch(this.handleError);
  }

  getActivity(id:string):Promise<activity> {

    const headers = new Headers({ 'Content-Type': 'application/json' });
    const options = new RequestOptions({ headers: headers });

    return this.http.post(this.httpUrl+"/getActivity", { id }, options)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }*/
}
